<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="实习前临时抱佛脚研究下编译原理，其实前端近些年不少重要的新技术其实是跟编译原理关系非常大。React在实现上有两个亮点：JSX、Virtual Dom，其中JSX就跟编译原理息息相关。想要提高自己的层次，未来定会好好补补基础。">
<meta property="og:type" content="article">
<meta property="og:title" content="前端与编译原理">
<meta property="og:url" content="https://hxy1997.xyz/2021/05/30/%E5%89%8D%E7%AB%AF%E4%B8%8E%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="实习前临时抱佛脚研究下编译原理，其实前端近些年不少重要的新技术其实是跟编译原理关系非常大。React在实现上有两个亮点：JSX、Virtual Dom，其中JSX就跟编译原理息息相关。想要提高自己的层次，未来定会好好补补基础。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531100810.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531101829.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531102906.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531103715.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531103748.png">
<meta property="article:published_time" content="2021-05-30T12:35:39.000Z">
<meta property="article:modified_time" content="2021-06-01T07:58:42.564Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="编译原理">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531100810.png">

<link rel="canonical" href="https://hxy1997.xyz/2021/05/30/%E5%89%8D%E7%AB%AF%E4%B8%8E%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>前端与编译原理 | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2021/05/30/%E5%89%8D%E7%AB%AF%E4%B8%8E%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          前端与编译原理
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-05-30 20:35:39" itemprop="dateCreated datePublished" datetime="2021-05-30T20:35:39+08:00">2021-05-30</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-06-01 15:58:42" itemprop="dateModified" datetime="2021-06-01T15:58:42+08:00">2021-06-01</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" itemprop="url" rel="index"><span itemprop="name">计算机基础知识</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/05/30/%E5%89%8D%E7%AB%AF%E4%B8%8E%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/05/30/%E5%89%8D%E7%AB%AF%E4%B8%8E%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>实习前临时抱佛脚研究下编译原理，其实前端近些年不少重要的新技术其实是跟编译原理关系非常大。React在实现上有两个亮点：JSX、Virtual Dom，其中JSX就跟编译原理息息相关。想要提高自己的层次，未来定会好好补补基础。</p>
<span id="more"></span>

<h2 id="1-编译原理基础内容"><a href="#1-编译原理基础内容" class="headerlink" title="1.编译原理基础内容"></a>1.编译原理基础内容</h2><h3 id="1-1-编译过程"><a href="#1-1-编译过程" class="headerlink" title="1.1 编译过程"></a>1.1 编译过程</h3><p>词法分析 =&gt; 语法分析 =&gt; 语义分析 （前端）=&gt; 中间代码生成 =&gt; 代码优化 =&gt; 目标代码生成 （后端）</p>
<h3 id="1-2-词法分析-gt-Token序列"><a href="#1-2-词法分析-gt-Token序列" class="headerlink" title="1.2 词法分析 =&gt; Token序列"></a>1.2 词法分析 =&gt; Token序列</h3><p>以乘法计算代码分析为例</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> answer = <span class="number">6</span> * <span class="number">7</span>;</span><br></pre></td></tr></table></figure>
<p>将用到的词列成表格，分别写出各部分的值和类型</p>
<table>
<thead>
<tr>
<th>type</th>
<th>value</th>
</tr>
</thead>
<tbody><tr>
<td>Keyword</td>
<td>var</td>
</tr>
<tr>
<td>Identifier</td>
<td>answer</td>
</tr>
<tr>
<td>Punctuator</td>
<td>=</td>
</tr>
<tr>
<td>Numeric</td>
<td>6</td>
</tr>
<tr>
<td>Punctuator</td>
<td>*</td>
</tr>
<tr>
<td>Numeric</td>
<td>7</td>
</tr>
<tr>
<td>Punctuator</td>
<td>;</td>
</tr>
</tbody></table>
<h3 id="1-3-语法分析-gt-AST（抽象语法树）"><a href="#1-3-语法分析-gt-AST（抽象语法树）" class="headerlink" title="1.3 语法分析 =&gt; AST（抽象语法树）"></a>1.3 <a target="_blank" rel="noopener" href="http://esprima.org/demo/parse.html">语法分析</a> =&gt; AST（抽象语法树）</h3><p>还是上述乘法计算</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var answer &#x3D; 6 * 7;</span><br></pre></td></tr></table></figure>

<p>这次将其展开成为AST抽象语法树</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531100810.png" alt="ast"></p>
<h3 id="1-4-语法解析器算法"><a href="#1-4-语法解析器算法" class="headerlink" title="1.4 语法解析器算法"></a>1.4 语法解析器算法</h3><p>分析形式包括</p>
<p>CSG、CFG</p>
<p>Top-Down Paring、Bottom-Up Parsing</p>
<p>Recursive Descent、Shift Reduce</p>
<p>LL(1)(GCC) 、LR(0)、LALR(1)、SLR(1)</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531101829.png" alt="parsing"></p>
<h3 id="1-5-JS文法"><a href="#1-5-JS文法" class="headerlink" title="1.5 JS文法"></a>1.5 <a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/jscore.jison">JS文法</a></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">Literal</span><br><span class="line">    : NULLTOKEN</span><br><span class="line">    | TRUETOKEN</span><br><span class="line">    | FALSETOKEN</span><br><span class="line">    | NUMBER</span><br><span class="line">    | STRING</span><br><span class="line">    | &#39;&#x2F;&#39;</span><br><span class="line">    | DIVEQUAL</span><br><span class="line">    ;</span><br><span class="line"></span><br><span class="line">Property</span><br><span class="line">    : IDENT &#39;:&#39; AssignmentExpr</span><br><span class="line">    | STRING &#39;:&#39; AssignmentExpr</span><br><span class="line">    | NUMBER &#39;:&#39; AssignmentExpr</span><br><span class="line">    | IDENT IDENT &#39;(&#39; &#39;)&#39; OPENBRACE FunctionBody CLOSEBRACE</span><br><span class="line">    | IDENT IDENT &#39;(&#39; FormalParameterList &#39;)&#39; OPENBRACE FunctionBody CLOSEBRACE</span><br><span class="line">    ;</span><br><span class="line"></span><br><span class="line">PropertyList</span><br><span class="line">    : Property</span><br><span class="line">    | PropertyList &#39;,&#39; Property</span><br><span class="line">    ;</span><br></pre></td></tr></table></figure>

<h3 id="1-6-其他待补充内容"><a href="#1-6-其他待补充内容" class="headerlink" title="1.6 其他待补充内容"></a>1.6 其他待补充内容</h3><h4 id="语义检查"><a href="#语义检查" class="headerlink" title="语义检查"></a>语义检查</h4><ol>
<li>变量是否声明</li>
<li>变量类型</li>
<li>类是否重定义</li>
<li>类里面的方法是否重定义</li>
<li>标识符里是否使用了保留字</li>
<li>…</li>
</ol>
<h4 id="中间代码生成"><a href="#中间代码生成" class="headerlink" title="中间代码生成"></a>中间代码生成</h4><h4 id="代码优化"><a href="#代码优化" class="headerlink" title="代码优化"></a>代码优化</h4><h4 id="目标代码生成"><a href="#目标代码生成" class="headerlink" title="目标代码生成"></a>目标代码生成</h4><p>前端到底跟编译原理有哪些关系？</p>
<h2 id="2-模板引擎：Jade"><a href="#2-模板引擎：Jade" class="headerlink" title="2.模板引擎：Jade"></a>2.模板引擎：<a target="_blank" rel="noopener" href="http://jade-lang.com/">Jade</a></h2><p>受Haml的影响以JavaScript实现用于node的高性能模板引擎</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">h1 Jade - node template engine</span><br><span class="line">#container.col</span><br><span class="line">    p.</span><br><span class="line">        Jade is a terse and simple</span><br><span class="line">        templating language with a</span><br><span class="line">        strong focus on performance</span><br><span class="line">        and powerful features.</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>html文件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Jade - node template engine<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;container&quot;</span> <span class="attr">class</span>=<span class="string">&quot;col&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line">        Jade is a terse and simple</span><br><span class="line">        templating language with a</span><br><span class="line">        strong focus on performance</span><br><span class="line">        and powerful features.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="2-1-词法分析：字符扫描器"><a href="#2-1-词法分析：字符扫描器" class="headerlink" title="2.1 词法分析：字符扫描器"></a>2.1 <a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/jadeTokens.json">词法分析</a>：字符扫描器</h3><h3 id="2-2-语法分析：Recursive-Descent"><a href="#2-2-语法分析：Recursive-Descent" class="headerlink" title="2.2 语法分析：Recursive Descent"></a>2.2 <a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/jadeNodeTree.json">语法分析</a>：Recursive Descent</h3><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531102906.png" alt="jade-ast"></p>
<p>建立类似于DOM树的AST，结点上附带相关信息，如属性，名称等</p>
<h3 id="2-3-代码生成"><a href="#2-3-代码生成" class="headerlink" title="2.3 代码生成"></a>2.3 <a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/jadeCompile.js">代码生成</a></h3><p>遍历上文中生成的AST树，生成JS代码。传入运行时环境，执行生成的函数，得到最终的模板输出</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> (<span class="params">locals, jade</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">var</span> buf = [];</span><br><span class="line">    <span class="keyword">var</span> jade_mixins = &#123;&#125;;</span><br><span class="line">    <span class="keyword">var</span> jade_interp;</span><br><span class="line"></span><br><span class="line">    jade_debug.unshift(<span class="keyword">new</span> jade.DebugItem(<span class="number">0</span>, <span class="string">&quot;jade.jade&quot;</span>));</span><br><span class="line">    jade_debug.unshift(<span class="keyword">new</span> jade.DebugItem(<span class="number">1</span>, <span class="string">&quot;jade.jade&quot;</span>));</span><br><span class="line">    buf.push(<span class="string">&quot;&lt;h1&gt;&quot;</span>);</span><br><span class="line">    jade_debug.unshift(<span class="keyword">new</span> jade.DebugItem(<span class="literal">undefined</span>, jade_debug[<span class="number">0</span>].filename));</span><br><span class="line">    jade_debug.unshift(<span class="keyword">new</span> jade.DebugItem(<span class="number">1</span>, jade_debug[<span class="number">0</span>].filename));</span><br><span class="line">    buf.push(<span class="string">&quot;Jade - node template engine&quot;</span>);</span><br><span class="line">    jade_debug.shift();</span><br><span class="line">    jade_debug.shift();</span><br><span class="line">    buf.push(<span class="string">&quot;&lt;/h1&gt;&quot;</span>);</span><br><span class="line">    ...</span><br><span class="line">    buf.push(<span class="string">&quot;&lt;/div&gt;&quot;</span>);</span><br><span class="line">    jade_debug.shift();</span><br><span class="line">    jade_debug.shift();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> buf.join(<span class="string">&quot;&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="3-代码验证：JSHint"><a href="#3-代码验证：JSHint" class="headerlink" title="3.代码验证：JSHint"></a>3.代码验证：<a target="_blank" rel="noopener" href="http://jshint.com/">JSHint</a></h2><p>Javascript代码验证工具，可以检查你的代码并提供相关的代码改进意见</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params">a, b</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">var</span> b = b || <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">add(<span class="number">5</span>, <span class="number">7</span>);</span><br></pre></td></tr></table></figure>

<p>jshint.js: line 3, col 9, ‘b’ is already defined.</p>
<p><a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/jshintTokens.json">词法分析</a>：字符扫描器</p>
<p>语法分析：<a target="_blank" rel="noopener" href="http://javascript.crockford.com/tdop/tdop.html">Top Down Operator Precedence</a></p>
<h3 id="3-1-JSHint-AST"><a href="#3-1-JSHint-AST" class="headerlink" title="3.1 JSHint AST"></a>3.1 JSHint AST</h3><p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531103715.png" alt="jshint-ast"></p>
<p>自顶向下，解析时检查代码错误</p>
<h2 id="4-代码压缩：UglifyJS"><a href="#4-代码压缩：UglifyJS" class="headerlink" title="4.代码压缩：UglifyJS"></a>4.代码压缩：UglifyJS</h2><p>使用JavaScript编写的JavaScript代码压缩器</p>
<ol>
<li>解析器，通过JavaScript代码产生AST(<a target="_blank" rel="noopener" href="http://marijnhaverbeke.nl//parse-js/">parse-js</a>)</li>
<li>代码生成器，通过AST生成JavaScript代码</li>
<li>压缩器，通过转换器对AST进行优化</li>
<li>变量名压缩器，将变量名转换为单个字母</li>
<li>作用域分析器，在AST上提供变量作用域相关信息</li>
<li>树遍历器，对AST的节点进行遍历操作</li>
<li>树转换器，对AST的树型结构进行转换</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">first, second</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> first + second;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span>(<span class="params">e,t</span>)</span>&#123;<span class="keyword">return</span> e+t&#125;;</span><br></pre></td></tr></table></figure>

<p>Input - Tokenize =&gt; Tokens - Parse =&gt; <a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/uglifyAST.json">AST</a> - Mangle =&gt; AST - Squeeze =&gt; AST（移除多余代码块、变量声明合并、改变语句写法）- Generate =&gt; Result</p>
<p><img data-src="https://cdn.jsdelivr.net/gh/huxingyi1997/my_img/img/20210531103748.png" alt="uglifyjs-ast"></p>
<h2 id="5-CSS预处理"><a href="#5-CSS预处理" class="headerlink" title="5.CSS预处理"></a>5.CSS预处理</h2><h3 id="5-1-以Less-js为例"><a href="#5-1-以Less-js为例" class="headerlink" title="5.1 以Less.js为例"></a>5.1 以<a target="_blank" rel="noopener" href="http://less2css.org/">Less.js</a>为例</h3><figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">@base:</span> <span class="number">#f938ab</span>;</span><br><span class="line"><span class="selector-class">.box-shadow</span>(<span class="variable">@style</span>, <span class="variable">@c</span>) <span class="keyword">when</span> (iscolor(<span class="variable">@c</span>)) &#123;</span><br><span class="line">  -webkit-<span class="attribute">box-shadow</span>: <span class="variable">@style</span> <span class="variable">@c</span>;</span><br><span class="line">  <span class="attribute">box-shadow</span>: <span class="variable">@style</span> <span class="variable">@c</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.box-shadow</span>(<span class="variable">@style</span>, <span class="variable">@alpha</span>: <span class="number">50%</span>) <span class="keyword">when</span> (isnumber(<span class="variable">@alpha</span>)) &#123;</span><br><span class="line">  <span class="selector-class">.box-shadow</span>(<span class="variable">@style</span>, rgba(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="variable">@alpha</span>));</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.box</span> &#123;</span><br><span class="line">  <span class="attribute">color</span>: saturate(<span class="variable">@base</span>, <span class="number">5%</span>);</span><br><span class="line">  <span class="attribute">border-color</span>: lighten(<span class="variable">@base</span>, <span class="number">30%</span>);</span><br><span class="line">  <span class="selector-tag">div</span> &#123; <span class="selector-class">.box-shadow</span>(<span class="number">0</span> <span class="number">0</span> <span class="number">5px</span>, <span class="number">30%</span>) &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* 编译结果 */</span></span><br><span class="line"><span class="selector-class">.box</span> &#123;</span><br><span class="line">  <span class="attribute">color</span>: <span class="number">#fe33ac</span>;</span><br><span class="line">  <span class="attribute">border-color</span>: <span class="number">#fdcdea</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.box</span> <span class="selector-tag">div</span> &#123;</span><br><span class="line">  -webkit-<span class="attribute">box-shadow</span>: <span class="number">0</span> <span class="number">0</span> <span class="number">5px</span> rgba(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.3</span>);</span><br><span class="line">  <span class="attribute">box-shadow</span>: <span class="number">0</span> <span class="number">0</span> <span class="number">5px</span> rgba(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.3</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-2-编译过程"><a href="#5-2-编译过程" class="headerlink" title="5.2 编译过程"></a>5.2 编译过程</h3><p>Input - parser =&gt; <a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/lessAST.json">AST</a> （注：Lexer被包含在Parser中）</p>
<p>Ruleset -&gt; Rule -&gt; Value -&gt; Expression -&gt; Entity</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.class</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#fff</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000</span>;</span><br><span class="line">    <span class="attribute">width</span>: @w + <span class="number">4px</span>;</span><br><span class="line">    &gt; <span class="selector-class">.child</span> &#123;...&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/* 对应的AST树 */</span></span><br><span class="line">Ruleset (Selector &#x27;.class&#x27;, [    Rule (&quot;color&quot;, Value ([Expression [Color #fff]]))</span><br><span class="line">    Rule (&quot;<span class="attribute">border</span>&quot;, Value (<span class="selector-attr">[Expression [Dimension 1px]</span><span class="selector-attr">[Keyword <span class="string">&quot;solid&quot;</span>]</span><span class="selector-attr">[Color #000]</span>]))</span><br><span class="line">    Rule (&quot;<span class="attribute">width</span>&quot;, Value (<span class="selector-attr">[Expression [Operation <span class="string">&quot; + &quot;</span> [Variable <span class="string">&quot;@w&quot;</span>]</span><span class="selector-attr">[Dimension 4px]</span>]]))</span><br><span class="line">    Ruleset (Selector <span class="selector-attr">[Element <span class="string">&#x27;&gt;&#x27;</span>, <span class="string">&#x27;.child&#x27;</span>]</span>, <span class="selector-attr">[...]</span>)</span><br><span class="line">])</span><br></pre></td></tr></table></figure>

<p>之后程序遍历AST，根据AST的内容同时进行函数注册，表达式求值等操作，生成新的AST，再根据新的AST树生成最终的CSS代码</p>
<h2 id="6-转译语言：CoffeeScript"><a href="#6-转译语言：CoffeeScript" class="headerlink" title="6.转译语言：CoffeeScript"></a>6.转译语言：<a target="_blank" rel="noopener" href="http://coffeescript.org/">CoffeeScript</a></h2><p>CoffeeScript是一套JavaScript的转译语言，会将类似 Ruby 语法的代码编译成 JavaScript，而且大部分结构都相似，但不同的是 CoffeeScript 拥有更严格的语法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">square = (x) -&gt; x * x</span><br><span class="line">cube = (x) -&gt; square(x) * x</span><br><span class="line"><span class="keyword">var</span> cube, square;</span><br><span class="line"></span><br><span class="line">square = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> x * x;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">cube = <span class="function"><span class="keyword">function</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> square(x) * x;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="6-1-不同之处"><a href="#6-1-不同之处" class="headerlink" title="6.1 不同之处"></a>6.1 不同之处</h3><p>Lexer相比前文介绍的工具，加入了少量正则表达式；</p>
<p>Parser使用语法解析器自动生成工具<a target="_blank" rel="noopener" href="https://github.com/zaach/jison">Jsion</a>（bison的Javascript版）</p>
<p><a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/grammar.coffee">CoffeeScript文法</a></p>
<figure class="highlight coffeescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">Root: [</span><br><span class="line">    o <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    o <span class="string">&#x27;Body&#x27;</span></span><br><span class="line">]</span><br><span class="line">Body: [</span><br><span class="line">    o <span class="string">&#x27;Line&#x27;</span>,</span><br><span class="line">    o <span class="string">&#x27;Body TERMINATOR Line&#x27;</span>,</span><br><span class="line">    o <span class="string">&#x27;Body TERMINATOR&#x27;</span></span><br><span class="line">]</span><br><span class="line">Line: [</span><br><span class="line">    o <span class="string">&#x27;Expression&#x27;</span></span><br><span class="line">    o <span class="string">&#x27;Statement&#x27;</span></span><br><span class="line">]</span><br><span class="line">Statement: [</span><br><span class="line">    o <span class="string">&#x27;Return&#x27;</span></span><br><span class="line">    o <span class="string">&#x27;Comment&#x27;</span></span><br><span class="line">    o <span class="string">&#x27;STATEMENT&#x27;</span></span><br><span class="line">]</span><br><span class="line">...</span><br></pre></td></tr></table></figure>



<h2 id="7-混写语言：React-JSX"><a href="#7-混写语言：React-JSX" class="headerlink" title="7.混写语言：React JSX"></a>7.混写语言：<a target="_blank" rel="noopener" href="https://facebook.github.io/react/jsx-compiler.html">React JSX</a></h2><p>JSX语法，像是在Javascript代码里直接写XML的语法，实质上这只是一个语法糖，每一个XML标签都会被JSX转换工具转换成纯Javascript代码</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> HelloMessage = React.createClass(&#123;</span><br><span class="line">    render: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Hello &#123;this.props.name&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">React.render(<span class="xml"><span class="tag">&lt;<span class="name">HelloMessage</span> <span class="attr">name</span>=<span class="string">&quot;John&quot;</span> /&gt;</span></span>, mountNode);</span><br><span class="line"><span class="keyword">var</span> HelloMessage = React.createClass(&#123;<span class="attr">displayName</span>: <span class="string">&quot;HelloMessage&quot;</span>,</span><br><span class="line">    render: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> React.createElement(<span class="string">&quot;div&quot;</span>, <span class="literal">null</span>, <span class="string">&quot;Hello &quot;</span>, <span class="built_in">this</span>.props.name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line">React.render(React.createElement(HelloMessage, &#123;<span class="attr">name</span>: <span class="string">&quot;John&quot;</span>&#125;), mountNode);</span><br></pre></td></tr></table></figure>

<p>使用开源JavaScript语法解析器<a target="_blank" rel="noopener" href="http://esprima.org/">esprima</a>，在源码中加入JSX相关的解析代码</p>
<p>使用修改后的JavaScript语法解析器来生成<a target="_blank" rel="noopener" href="https://test.surunzi.com/slide/compiler/doc/jsxAST.json">AST</a>，再遍历AST结点生成最终转换后的Js代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;X&gt; &lt;&#x2F;X&gt; &#x3D;&gt; React.createElement(X, null)</span><br><span class="line">&lt;X prop&#x3D;&quot;1&quot; &#x2F;&gt; &#x3D;&gt; React.createElement(X, &#123;prop: &#39;1&#39;&#125;, null)</span><br><span class="line">&lt;X prop&#x3D;&quot;2&quot;&gt;&lt;Y &#x2F;&gt;&lt;&#x2F;X&gt; &#x3D;&gt; React.createElement(X, &#123;prop:&#39;2&#39;&#125;,</span><br><span class="line">    React.createElement(Y, null)</span><br><span class="line">)</span><br><span class="line">&lt;div &#x2F;&gt; &#x3D;&gt; React.createElement(&quot;div&quot;, null)</span><br></pre></td></tr></table></figure>



<h2 id="其它应用方向"><a href="#其它应用方向" class="headerlink" title="其它应用方向"></a>其它应用方向</h2><ol>
<li><a target="_blank" rel="noopener" href="https://github.com/surunzi/WebVN">游戏引擎脚本语言</a></li>
<li>Markdown转换</li>
<li>…</li>
</ol>
<h2 id="拓展"><a href="#拓展" class="headerlink" title="拓展"></a>拓展</h2><ol>
<li>V8 <a target="_blank" rel="noopener" href="https://github.com/v8/v8/blob/master/src/scanner.cc">Lexer</a>、<a target="_blank" rel="noopener" href="https://github.com/v8/v8/blob/master/src/parser.cc">Parser</a></li>
<li>Rhino <a target="_blank" rel="noopener" href="https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/TokenStream.java">Lexer</a>、<a target="_blank" rel="noopener" href="https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/Parser.java">Parser</a></li>
</ol>

    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"><i class="fa fa-tag"></i> 前端</a>
              <a href="/tags/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86/" rel="tag"><i class="fa fa-tag"></i> 编译原理</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/05/13/TypeScript%E9%87%8D%E6%9E%84axios/" rel="prev" title="TypeScript重构axios">
      <i class="fa fa-chevron-left"></i> TypeScript重构axios
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/06/02/%E5%B8%B8%E7%94%A8%E7%9A%84webpack%E4%BC%98%E5%8C%96%E6%96%B9%E6%B3%95/" rel="next" title="常用的webpack优化方法">
      常用的webpack优化方法 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86%E5%9F%BA%E7%A1%80%E5%86%85%E5%AE%B9"><span class="nav-text">1.编译原理基础内容</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B"><span class="nav-text">1.1 编译过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90-gt-Token%E5%BA%8F%E5%88%97"><span class="nav-text">1.2 词法分析 &#x3D;&gt; Token序列</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90-gt-AST%EF%BC%88%E6%8A%BD%E8%B1%A1%E8%AF%AD%E6%B3%95%E6%A0%91%EF%BC%89"><span class="nav-text">1.3 语法分析 &#x3D;&gt; AST（抽象语法树）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E8%AF%AD%E6%B3%95%E8%A7%A3%E6%9E%90%E5%99%A8%E7%AE%97%E6%B3%95"><span class="nav-text">1.4 语法解析器算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-JS%E6%96%87%E6%B3%95"><span class="nav-text">1.5 JS文法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6-%E5%85%B6%E4%BB%96%E5%BE%85%E8%A1%A5%E5%85%85%E5%86%85%E5%AE%B9"><span class="nav-text">1.6 其他待补充内容</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AF%AD%E4%B9%89%E6%A3%80%E6%9F%A5"><span class="nav-text">语义检查</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%AD%E9%97%B4%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90"><span class="nav-text">中间代码生成</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E4%BC%98%E5%8C%96"><span class="nav-text">代码优化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90"><span class="nav-text">目标代码生成</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%A8%A1%E6%9D%BF%E5%BC%95%E6%93%8E%EF%BC%9AJade"><span class="nav-text">2.模板引擎：Jade</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E8%AF%8D%E6%B3%95%E5%88%86%E6%9E%90%EF%BC%9A%E5%AD%97%E7%AC%A6%E6%89%AB%E6%8F%8F%E5%99%A8"><span class="nav-text">2.1 词法分析：字符扫描器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E8%AF%AD%E6%B3%95%E5%88%86%E6%9E%90%EF%BC%9ARecursive-Descent"><span class="nav-text">2.2 语法分析：Recursive Descent</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90"><span class="nav-text">2.3 代码生成</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BB%A3%E7%A0%81%E9%AA%8C%E8%AF%81%EF%BC%9AJSHint"><span class="nav-text">3.代码验证：JSHint</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-JSHint-AST"><span class="nav-text">3.1 JSHint AST</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%BB%A3%E7%A0%81%E5%8E%8B%E7%BC%A9%EF%BC%9AUglifyJS"><span class="nav-text">4.代码压缩：UglifyJS</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-CSS%E9%A2%84%E5%A4%84%E7%90%86"><span class="nav-text">5.CSS预处理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E4%BB%A5Less-js%E4%B8%BA%E4%BE%8B"><span class="nav-text">5.1 以Less.js为例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B"><span class="nav-text">5.2 编译过程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E8%BD%AC%E8%AF%91%E8%AF%AD%E8%A8%80%EF%BC%9ACoffeeScript"><span class="nav-text">6.转译语言：CoffeeScript</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-%E4%B8%8D%E5%90%8C%E4%B9%8B%E5%A4%84"><span class="nav-text">6.1 不同之处</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-%E6%B7%B7%E5%86%99%E8%AF%AD%E8%A8%80%EF%BC%9AReact-JSX"><span class="nav-text">7.混写语言：React JSX</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B6%E5%AE%83%E5%BA%94%E7%94%A8%E6%96%B9%E5%90%91"><span class="nav-text">其它应用方向</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%93%E5%B1%95"><span class="nav-text">拓展</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
